﻿using FadeLang.Ast;
using FadeLang.Runtime;

namespace FadeLang.CstVisitor
{
    public class ExpVisitor : FadeLangBaseVisitor<Exp>
    {
        private readonly Context context;

        public ExpVisitor(Context context) {
            this.context = context;
        }

        public override Exp VisitExp(FadeLangParser.ExpContext context) {
            if (context == null) {
                return null;
            }

            var exp = new Exp();
            if (context.NAME() != null) {
                exp.Name = context.NAME().GetText();
                return exp;
            }
            if (context.literals() != null) {
                var value = LiteralsVisitor.Instance.VisitLiterals(context.literals());
                exp.Value = value;
                return exp;
            }
            if (context.functionCall() != null) {
                exp.FunctionCall = context.functionCall().Accept(new FunctionCallVisitor(this.context));
                return exp;
            }

            exp.LeftExp = VisitExp(context.exp(0));
            exp.RightExp = VisitExp(context.exp(1));
            exp.Op = OpTypeConverter.Parse(context.op.Text);
            return exp;
        }
    }
}